// 确定剪裁区
let image = $('#image');
// - 设置配置项
let option = {

    aspectRatio: 1,
    // 指定预览区域
    preview: '.img-preview'
};

image.cropper(option);


// 点击按钮，上传图片
$('.upto').on('click', function () {
    $('#file').trigger('click')
})


// 选取图片
$('#file').on('change', function () {

    if (this.files.length > 0) {
        let fileobj = this.files[0]
        let url = URL.createObjectURL(fileobj)
        // 先销毁图片的剪裁区，然后更换，最后生成新的剪裁框
        image.cropper('destroy').attr('src', url).cropper(option)
    }
})

let layer = layui.layer
// 确认图片，准备提交
$('.uppic').on('click', function () {
    let canvas = image.cropper('getCroppedCanvas', { width: 30, height: 30 })
    let base64 = canvas.toDataURL('image/jpeg', 0.5)
    $.ajax({
        url: '/my/user/avatar',
        type: 'POST',
        data: { avatar: base64 },
        success: function (res) {
            layer.msg(res.message)
            if (res.status === 0) {
                window.parent.renderuser()

            }
        }
    })
})
